package Test;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @Date: Created in 2023/4/11 13:48
 */
public class PI_ extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        MenuBar menuBar=new MenuBar();
        Menu menu1=new Menu("算圆的面积");
        Menu menu2=new Menu("算圆的周长");
        MenuItem menuItem1=new MenuItem("计算面积");
        MenuItem menuItem2=new MenuItem("计算周长");
        menuItem1.setOnAction(event -> {
            Label label1=new Label("请输入圆的半径:");
            label1.setLayoutY(100);
            label1.setLayoutX(50);
            Label label2=new Label("圆的面积是:");
            label2.setLayoutY(300);
            label2.setLayoutX(50);
            TextField textField1=new TextField();
            textField1.setLayoutY(100);
            textField1.setLayoutX(140);
            TextField textField2=new TextField();
            textField2.setLayoutY(300);
            textField2.setLayoutX(120);
            textField1.setOnAction(event1 -> {
                double r=Double.valueOf(textField1.getText());
                double s=PI_.cal2()*r*r;
                textField2.setText(String.valueOf(s));
            });
            AnchorPane pane2=new AnchorPane();
            pane2.getChildren().addAll(label1,label2,textField1,textField2);
            Scene scene=new Scene(pane2,600,600);
            Stage stage=new Stage();
            stage.setScene(scene);
            stage.show();
        });

        menu1.getItems().add(menuItem1);
        menu2.getItems().add(menuItem2);
        menuBar.getMenus().addAll(menu1,menu2);
        AnchorPane pane=new AnchorPane();
        pane.getChildren().add(menuBar);
        Scene scene=new Scene(pane,400,400);
        primaryStage.setScene(scene);
        menuBar.setPrefWidth(400);
        primaryStage.show();
    }
    public static double cal1(){
        //莱布尼茨公式
        double sum=0;
        for(int i=0;i<10000;i++){
            if(i%2==0){
                sum+=1.0/(2*i+1);
            }else {
                sum+=1.0/(2*i+1)*(-1);
            }
        }
        return 4*sum;
    }
    public static double cal2(){
        //割圆法
        double s=0.0;
        double r=1.0;
        double pi=0.0;
        for(int i=2;i<=100;i++){
            double n=Math.pow(2,i);
            s=n*1/2*Math.pow(r,i)*Math.sin(Math.toRadians(360/n));
            pi=s/Math.pow(r,2);
        }
        return pi;
    }
}
